package com.cpit.platform.service.impl;

import com.cpit.platform.common.constant.LogConstant;
import com.cpit.platform.common.dao.SimpleExpression;
import com.cpit.platform.common.entity.Result;
import com.cpit.platform.common.entity.TableDataQueryCondition;
import com.cpit.platform.common.entity.TableJsonData;
import com.cpit.platform.dao.EntityRepository;
import com.cpit.platform.dao.OperLogRepository;
import com.cpit.platform.dto.LogModule;
import com.cpit.platform.dto.LogOperItem;
import com.cpit.platform.dto.OperLog;
import com.cpit.platform.service.OperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author Yanyongqing
 * @since 2018/3/21.
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OperLogServiceImpl extends EntityServiceImpl<OperLog> implements OperLogService {
    @Autowired
    private OperLogRepository operLogRepository;

    @Override
    protected EntityRepository getEntityRepository() {
        return operLogRepository;
    }

    @Override
    public Result<OperLog> writeLog(OperLog operLog) {
        operLog.setCommandName(LogConstant.OPER_MAP.get(operLog.getCommand()));
        operLog.setModuleName(LogConstant.MODULE_MAP.get(operLog.getModule()));
        if (Result.CODE_SUCCESS.equals(operLog.getResult())) {
            operLog.setResultName("成功");
        } else if (Result.CODE_FAILED.equals(operLog.getResult())) {
            operLog.setResultName("失败");
        } else if (Result.CODE_PARTIAL_SUCCESS.equals(operLog.getResult())) {
            operLog.setResultName("部分成功");
        }
        return this.create(operLog);
    }

    @Override
    public TableJsonData queryLogByCondition(TableDataQueryCondition cond) {
        PageRequest pageable = TableDataQueryCondition.getPageRequestByTableDataQueryCondition(cond);
        //一、处理原有Map查询条件,将其放入简单条件列表中统一处理
        Map queryCondMap = cond.getQueryCond();
        List<SimpleExpression> queryCondSimple = cond.getQueryCondSimple();
        Page<OperLog> ret = this.findByQueryCondition(queryCondMap, queryCondSimple, pageable);

        return new TableJsonData(cond, ret.getTotalElements(), ret.getContent());
    }

    @Override
    public List<LogModule> getAllModules() {
        List<LogModule> modules = new ArrayList<LogModule>(LogConstant.MODULE_MAP.size());
        Iterator<String> iter = LogConstant.MODULE_MAP.keySet().iterator();
        while (iter.hasNext()) {
            String key = iter.next();
            LogModule module = new LogModule();
            module.setModule(key);
            module.setModuleName(LogConstant.MODULE_MAP.get(key));
            modules.add(module);
        }
        return modules;
    }

    @Override
    public List<LogOperItem> getAllOperItem() {
        List<LogOperItem> operItems = new ArrayList<LogOperItem>(LogConstant.OPER_MAP.size());
        Iterator<String> iter = LogConstant.OPER_MAP.keySet().iterator();
        while (iter.hasNext()) {
            String key = iter.next();
            LogOperItem operItem = new LogOperItem();
            operItem.setCommand(key);
            operItem.setCommandName(LogConstant.OPER_MAP.get(key));
            operItems.add(operItem);
        }

        return operItems;
    }
}
